const middleWare1 = async (req, res, next) => {
  console.log('middleWare1 start');
  const result = await next?.()
  console.log('middleWare1 end，', result);
}

const middleWare2 = async (req, res, next) => {
  console.log('middleWare2 start');
  const result = await next?.()
  console.log('middleWare2 end，', result);
  return Promise.resolve('middleWare2 result')
}

const middleWare3 = async (req, res, next) => {
  console.log('middleWare3 start');
  await next?.()
  console.log('middleWare3 end');
  return Promise.resolve('middleWare3 result')
}

const middleWares = [middleWare1, middleWare2, middleWare3]

const run = (req, res) => {
  const next = () => {
    const middleWare = middleWares.shift()
    if (middleWare) {
      return Promise.resolve(middleWare(req, res, next))
    }
  }
  next()
}

run()
